import { defineStore } from 'pinia'
import { Caregory, Banner, New, Hot } from '@/types/home'
import request from '@/utils/request'
import { ApiRes } from '@/types/data'

/**
 * 仓库的名字必须唯一
 */
export const useHomeStore = defineStore('home', {
  state: () => {
    return {
      categories: [] as Caregory[], //分类列表数据
      bannerList: [] as Banner[], // 轮播图数据
      newList: [] as New[], // 新鲜好物
      hotList: [] as Hot[] // 人气推荐
    }
  },
  actions: {
    async getCategoryList () {
      const res = await request.get<ApiRes<Caregory[]>>('home/category/head')
      this.categories = res.data.result
    },
    async getBannerList () {
      const res = await request.get<ApiRes<Banner[]>>('home/banner')

      this.bannerList = res.data.result
    },
    async getNewList () {
      const res = await request.get<ApiRes<New[]>>('home/new')

      this.newList = res.data.result
    },
    async getHotList () {
      const res = await request.get<ApiRes<Hot[]>>('home/hot')

      this.hotList = res.data.result
    }
  }
})
